package ren.nearby.http.di;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wukewei on 16/5/26.
 */
public class SchedulersCompat {


    /*  private final static Observable.Transformer ioTransformer = new Observable.Transformer() {
          @Override
          public Object call(Object o) {
              return ((Observable) o)
                      .subscribeOn(Schedulers.io())
                      // Be notified on the main thread
                      .observeOn(AndroidSchedulers.mainThread())


          }
      };

      public static <T> Observable.Transformer<T, T> applyIoSchedulers() {
          return (Observable.Transformer<T, T>) ioTransformer;
      }*/
    public static final ObservableTransformer IO_TRANSFORMER = new ObservableTransformer() {
        @Override
        public ObservableSource apply(@NonNull Observable upstream) {
            return upstream.subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
        }
    };

    public static final <T> ObservableTransformer<T, T> applySchedulers() {
        return (ObservableTransformer<T, T>) IO_TRANSFORMER;
    }

    public static <T> ObservableTransformer<T, T> toMain() {

        return new ObservableTransformer<T, T>() {

            @Override
            public ObservableSource<T> apply(Observable<T> upstream) {
                return upstream.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }


}
